package com.wzq.permission.center.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wzq.framework.util.UtilValidate;
import com.wzq.permission.center.mapper.DataPermissionMapper;
import com.wzq.permission.client.model.Pager;
import com.wzq.permission.client.model.admin.DataPermissionModel;
import com.wzq.permission.client.service.DataPermissionService;
/**
 * 
*<p>Title: </p>
*<p>Description: </p>
*<p>Company: </p> 
*@version 
*@author  qianzq
*@date 2016年2月25日  上午9:45:05
 */
@Service
@Transactional
public class DatapermissionServiceImpl implements DataPermissionService{
	
	@Autowired
	private DataPermissionMapper dataMapper;
	
	@Override
	public void add(DataPermissionModel data) {
		dataMapper.add(data);
		
	}

	@Override
	public void update(DataPermissionModel data) {
		dataMapper.update(data);
		
	}

	@Override
	public void updateBySelective(DataPermissionModel data) {
		dataMapper.updateBySelective(data);
		
	}

	@Override
	public void delete(Object id) {
		dataMapper.delete(id);
		
	}

	@Override
	public int queryByCount(DataPermissionModel data) {
		return dataMapper.queryByCount(data);
	}

	@Override
	public List<DataPermissionModel> queryList(DataPermissionModel data, Pager pager) {
		
		if(StringUtils.isNotEmpty(data.getDataPermissionName())){
			data.setDataPermissionName(data.getDataPermissionName()+"%");
		}
		List<DataPermissionModel> dataList=dataMapper.quaryListByApplication(data, pager);
		int length=dataList.size();
		if(length>0){
			for (int i = 0; i < length; i++) {
				if(UtilValidate.isNotEmpty(dataList.get(i).getRangeType()) && dataList.get(i).getRangeType().equals("2")){//省  有时间在优化为 枚举类型
					dataList.get(i).setRangeType("省");
				}else if(UtilValidate.isNotEmpty(dataList.get(i).getRangeType()) && dataList.get(i).getRangeType().equals("3")){//市
					dataList.get(i).setRangeType("市");
				}else if(UtilValidate.isNotEmpty(dataList.get(i).getRangeType()) && dataList.get(i).getRangeType().equals("4")){//区县
					dataList.get(i).setRangeType("区、县");
				}else if(UtilValidate.isNotEmpty(dataList.get(i).getRangeType()) && dataList.get(i).getRangeType().equals("5")){//小区
					dataList.get(i).setRangeType("小区");
				}
			}
		}
		return dataMapper.quaryListByApplication(data, pager);
	}

	@Override
	public DataPermissionModel queryById(Object id) {
		return dataMapper.queryById(id);
	}

	@Override
	public void logicDelete(Long[] dataPermissionIds) {
		/*for(Long userId:userIds){
			//删除用户关联的系统
			applicationUserMapper.deleteByUserId(userId);
			//删除用户关联的角色
			userRoleMapper.deleteByUserAndAppId(userId, null);
			//根据用户删除组织管理
			userOrganizeMapper.deleteUserOrgByUserId(userId);
		}*/
		dataMapper.logicDelete(dataPermissionIds);
		
	}

    /* (非 Javadoc)  
     * <p>Title: isExistPermissionName</p>    
     * <p>Description:  TODO(描述) </p>    
     * @param model
     * @return    
     * @see com.wzq.permission.client.service.DataPermissionService#isExistPermissionName(com.wzq.permission.client.model.admin.DataPermissionModel)    
     */
    @Override
    public int isExist(DataPermissionModel model) {
        return dataMapper.isExist(model);
    }
}
